package com.github.shiqiyue.app.api.sapi.controller.app;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.req.notice.NoticePageReq;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.result.SimplePageResult;
import com.github.shiqiyue.app.modules.app.entity.Notice;
import com.github.shiqiyue.app.modules.app.enums.NoticeStatusEnum;
import com.github.shiqiyue.app.modules.app.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;

/***
 * notice 接口
 * @author wenyao.wu
 * @since 2020/6/17 0017
 */
@RestController
@RequestMapping("sapi/app/notice")
public class SApiNoticeController extends SApiBaseController {

    @Autowired
    private INoticeService noticeService;

    /***
     * 分页
     * @param noticePageReq
     * @param bindingResult
     * @return
     */
    @PostMapping("page")
    public Result page(@Valid NoticePageReq noticePageReq, BindingResult bindingResult) throws ParseException {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        IPage<Notice> resultPage = noticeService.page(noticePageReq);
        SimplePageResult<Notice> result = new SimplePageResult();
        result.setPageInfo(resultPage);
        result.setRecords(resultPage.getRecords());
        return Result.success(result);
    }

    /***
     * 添加
     * @param notice
     * @return
     */
    @PostMapping("add")
    public Result add(Notice notice) {
        notice.setStatus(NoticeStatusEnum.OFF.getValue());
        noticeService.save(notice);
        return Result.success();
    }

    /***
     * 详情
     * @param id
     * @return
     */
    @PostMapping("detail")
    public Result detail(Long id) {
        Assert.notNull(id);
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    /***
     * 修改
     * @param notice
     * @return
     */
    @PostMapping("edit")
    public Result edit(Notice notice) {
        noticeService.updateById(notice);
        return Result.success();
    }

    /***
     * 上架
     * @param id
     * @return
     */
    @PostMapping("on")
    public Result on(Long id) {
        Assert.notNull(id);

        Notice notice = noticeService.getById(id);
        notice.setStatus(NoticeStatusEnum.ON.getValue());
        noticeService.updateById(notice);

        return Result.success();
    }

    /***
     * 下架
     * @param id
     * @return
     */
    @PostMapping("off")
    public Result off(Long id) {
        Assert.notNull(id);

        Notice notice = noticeService.getById(id);
        notice.setStatus(NoticeStatusEnum.OFF.getValue());
        noticeService.updateById(notice);

        return Result.success();
    }

}
